package com.odais.Reservation;

import java.util.ArrayList;

import android.R.integer;

/**
 *
 * @author Lionel DECELLE
 * @since 12 juin 2012 - 16:52:59
 *
 */
public class ReservationList
{
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

public ArrayList<Reservation> reservationArrayList;
private ArrayList<Reservation> tmpArrayList;
private String date, hour;
private int minDate, minHour, minDateIndex, tmpListCnt, currentDate, currentHour;
private String listType;

// ===========================================================
// Constructors
// ===========================================================

public ReservationList() {
	super();
	tmpArrayList = new ArrayList<Reservation>();
	listType = new String("Undefined");
}

// ===========================================================
// Getter & Setter
// ===========================================================
	public String getListType() {
		return listType;
	}
	
	public void setListType(String listType) {
		this.listType = listType;
	}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

	

	public void create()
	{
		reservationArrayList = new ArrayList<Reservation>();
	}

	public void clear()
	{
		reservationArrayList.clear();
	}

	public void add(Reservation reservation)
	{
		reservationArrayList.add(reservation);
	}

	public void remove(int index)
	{
		reservationArrayList.remove(index);
	}

	public Reservation get(int i)
	{
		if(reservationArrayList!=null)
			return reservationArrayList.get(i);
		else
			return new Reservation("", "", 0, 0, "", 0, "", 0, "");
	}
 
	public int size() {
		if(reservationArrayList!=null)
		{
			return reservationArrayList.size();
		}
		else
			return 0;
	}
	
	public void sortByDate() {
		if(reservationArrayList!=null)
		{
			if(reservationArrayList.size()>0)
			{
				minDateIndex = 0;
				tmpListCnt = 0;
				tmpArrayList.clear();
				while(reservationArrayList.size()>0)
				{
					minDate = 0x3fffffff;
					minHour = 0xffffff;
					for(int i=0; i<reservationArrayList.size(); i++)
					{
						date = reservationArrayList.get(i).getItemDateRsv();
						hour = reservationArrayList.get(i).getItemHourRsv();
						currentDate = Integer.parseInt(date);
						currentHour = Integer.parseInt(hour);
	
						if(currentDate<minDate)
						{	
							minDate = currentDate;
							minDateIndex = i;
						}
						else if(currentDate==minDate)
						{
							if(currentHour<minHour)
							{
								minDate = currentDate;
								minHour = currentHour;
								minDateIndex = i;
							}
						}
					}
					//put the early date in the smallest position
					//tmpArrayList.set(tmpListCnt++, reservationArrayList.get(minDateIndex));
					tmpArrayList.add(reservationArrayList.get(minDateIndex));
					reservationArrayList.remove(minDateIndex);
				}
				//refresh the real list with the temporary one...
				reservationArrayList.clear();
				reservationArrayList = (ArrayList<Reservation>) tmpArrayList.clone();
			}
		}
	}

}